/*8vqHVAflEMt5ihJGRutYS2is7UN34eqMwauiNbGSXYBj15VNuaisTR5Or0wEjGpm*/
/*
 * @Author: xiahui 
 * @PageInfo: 批量操作工具：提交、收回 
 * @Date: 2019-01-18 13:26:16 
 * @Last Modified by: zhr
 * @Last Modified time: 2021-03-22 15:01:04
 */
import { ajax,toast,promptBox, getBusinessInfo } from 'nc-lightapp-front';
import { showWarningInfo, showSuccessInfo, showBatchOperateInfo } from '../../../../scmpub/scmpub/pub/tool/messageUtil';
import { BACKENDKEY, AREA, FIELDS } from '../constance';
import { setCardCompareData } from './cardPageUtil';
import { setListCompareData } from './listPageUtil';
import ScriptActionDlg from '../../pub/components/ScriptActionDlg';
import { getLangByResId } from '../../../../scmpub/scmpub/pub/tool/multiLangUtil';

// 差异操作参数
const config = {
	headAreaId: AREA.cardFormId,
	bodyIdAndPkMap: new Map([
		[ FIELDS.pk_ct_pu_b, AREA.cardTableId ],
		[ FIELDS.pk_ct_pu_term, AREA.cardTermId ],
		[ FIELDS.pk_ct_pu_payment, AREA.cardPayId ],
		[ FIELDS.pk_ct_pu_exp, AREA.cardFeeId ],
		[ FIELDS.pk_ct_pu_memora, AREA.cardMemoraId ],
		[ FIELDS.pk_ct_pu_change, AREA.cardChangeId ],
		[ FIELDS.pk_ct_pu_exec, AREA.cardExecutId ]
	]),
	baseBack: true // 基于后台数据更新,即使用updateDiffDataByRowId更新界面
};

/**
 * 列表批处理：提交、收回、生效、取消生效、冻结、解冻、终止、取消终止
 * @param {*} props 
 * @param {*} url 
 * @param {*} title 
 * @param {*} record 
 * @param {*} index 
 * @param {*} userObj 执行原因
 * @param {*} assign 指派
 */
function listBaseScriptAction(props, url, title, record, index, userObj, assign) {
	let reqData = { infos: [], userObj: userObj };
	let checkArr = [];
	if (record && index >= 0) {
		// 操作列提交
		reqData.infos.push({
			id: record[FIELDS.pk_ct_pu].value,
			ts: record[FIELDS.ts].value
		});
	} else {
		checkArr = props.table.getCheckedRows(AREA.listTableId);
		if (!checkArr || checkArr.length < 1) {
			showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000058')); /* 国际化处理： 请选择数据*/
			return;
		}

		checkArr.map((row) => {
			reqData.infos.push({
				id: row.data.values[FIELDS.pk_ct_pu].value,
				ts: row.data.values[FIELDS.ts].value
			});
		});
	}

	if (assign) {
		reqData['assign'] = JSON.stringify(assign);
	}

	ajax({
		url: url,
		data: reqData,
		success: (res) => {
			if (res.success && res.data) {
				let listParam = { checkArr: checkArr, record: record, index: index };
				processResData.call(this, props, res.data, title, listParam);
			}
		}
	});
}

/**
 * 列表批处理：提交、收回、生效、取消生效、冻结、解冻、终止、取消终止
 * @param {*} props 
 * @param {*} url 
 * @param {*} title 
 * @param {*} userObj
 * @param {*} assign 指派
 */
function cardBaseScriptAction(props, url, title, userObj, assign, skipCodes, saveCallback) {
	let rows = props.cardTable.getAllRows(AREA.cardTableId);
	let bodys = [];
	rows.forEach((row) => {
		bodys.push({
			id: row.values[FIELDS.pk_ct_pu_b].value,
			ts: row.values[FIELDS.ts].value
		});
	});
	let reqData = {
		infos: [
			{
				id: props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_ct_pu).value,
				ts: props.form.getFormItemsValue(AREA.cardFormId, FIELDS.ts).value,
				bodys: bodys
			}
		],
		userObj: userObj
	};

	skipCodes = skipCodes ? skipCodes : new Array();
	reqData['skipCodes'] = skipCodes;

	if (assign) {
		reqData['assign'] = JSON.stringify(assign);
	}
	console.log("生效前");
	ajax({
		url: url,
		data: reqData,
		success: (res) => {
			if (res.success && res.data) {
				processResData.call(this, props, res.data, title);
				if (saveCallback) {
					saveCallback();
				}
				if(title == "生效成功"){
					let code = props.form.getFormItemsValue('head','vtrantypecode').value;
					let rows = props.cardTable.getAllRows(AREA.cardTableId);
					if(code == "Z2-Cxx-008"){
						rows.forEach((row) => {
							let nutsum = row.values.norigmny.value;
							if(nutsum == '0.00'){
								promptBox({
									color: 'warning',
									title:"提示",
									content:"金额不能为空，无法自动生成采购订单，请手动生成！",
									noFooter:false,
									noCancelBtn:true,
									beSureBtnName:"是",
									
									beSureBtnClick:()=>{
									//             //同意
									//             props.cardTable.setValByKeyAndIndex(moduleId,i,'defitem15',{ value : "Y", display : "是"});
									//             pageButtonClick.call(this)[pageButton.pageSave]();//重新调用保存
									},
									//         cancelBtnClick: ()=>{
									//             //不同意
									//             props.cardTable.setValByKeyAndIndex(moduleId,i,'defitem15',{ value : "N", display : "否"});
									//             pageButtonClick.call(this)[pageButton.pageSave]();//重新调用保存
									//         }
								})
								// toast({content: "金额不能为空，无法自动生成采购订单，请手动生成！", color: 'warning'});
							}
						});
					}
				}
			}
		}
	});
	console.log("生效后");
}

/**
 * 处理后台返回数据
 * @param {*} props 
 * @param {*} data 
 * @param {*} title 
 * @param {*} listParam 
 */
function processResData(props, data, title, listParam) {
	if (
		data.userObj &&
		data.userObj.workflow &&
		(data.userObj.workflow == 'approveflow' || data.userObj.workflow == 'workflow')
	) {
		// 缓存当前数据 modify by huoyzh
		if (listParam) {
			this.commitInfo = { isBatch: true, record: listParam.record, index: listParam.index };
		} else {
			this.commitInfo = { isBatch: true };
		}

		this.setState({
			compositedata: data.userObj,
			compositedisplay: true
		});
	} else {
		// 数据更新
		if (data.successResult) {
			let resData = data.successResult[BACKENDKEY.successKey];
			if (resData) {
				if (listParam) {
					// 列表态操作
					setListCompareData.call(this, props, resData, listParam, config);
				} else {
					// 卡片态操作
					setCardCompareData.call(this, props, resData, config);
				}
			}
		}
		// 提示信息
		if (data.errMsg) {
			showBatchOperateInfo(null, data.message, data.errMsg);
		} else {
			listParam ? showSuccessInfo(title, data.message) : showSuccessInfo(title); /* 国际化处理title*/
		}
	}
}

/**
 * 列表脚本操作时，显示提示原因面板
 * @param {*} props 
 * @param {*} url 
 * @param {*} sucTitle 
 * @param {*} record 
 * @param {*} index 
 * @param {*} scrTitle 
 */
function showListScriptActionDlg(props, url, sucTitle, record, index, scrTitle) {
	this.reason = null;
	props.modal.show('ScriptActionDlg', {
		title: getLangByResId(this, '4004PURDAILY-000024') /* 国际化处理： 原因*/,
		hasCloseBtn: true,
		userControl: false,
		content: <ScriptActionDlg changeData={this.changeReason.bind(this)} title={scrTitle} />,
		beSureBtnClick: () => {
			listBaseScriptAction.call(this, props, url, sucTitle, record, index, { reason: this.reason });
		},
		cancelBtnClick: () => {
			//listBaseScriptAction.call(this, props, url, sucTitle, record, index);
		}
	});
}

/**
 * 卡片脚本操作时，显示提示原因面板
 * @param {*} props 
 * @param {*} url 
 * @param {*} sucTitle 
 * @param {*} record 
 * @param {*} index 
 * @param {*} scrTitle 
 */
function showCardScriptActionDlg(props, url, sucTitle, scrTitle) {
	this.reason = null;
	props.modal.show('ScriptActionDlg', {
		title: getLangByResId(this, '4004PURDAILY-000024') /* 国际化处理： 原因*/,
		hasCloseBtn: true,
		userControl: false,
		content: <ScriptActionDlg changeData={this.changeReason.bind(this)} title={scrTitle} />,
		beSureBtnClick: () => {
			cardBaseScriptAction.call(this, props, url, sucTitle, { reason: this.reason });
		},
		cancelBtnClick: () => {
			//cardBaseScriptAction.call(this, props, url, sucTitle);
		}
	});
}

/**
 * 是否显示弹框
 * @param {*} billDate 合同单据时间
 */
function isShowScriptActionDlg(billDate) {
	let businessDate = getBusinessInfo().businessDate; // 业务日期时间
	// 日期相同，不显示弹框
	if (billDate.split(' ')[0] == businessDate.split(' ')[0]) return false;
	return true;
}

export {
	listBaseScriptAction,
	cardBaseScriptAction,
	showListScriptActionDlg,
	showCardScriptActionDlg,
	isShowScriptActionDlg
};

/*8vqHVAflEMt5ihJGRutYS2is7UN34eqMwauiNbGSXYBj15VNuaisTR5Or0wEjGpm*/